<template>
  <div>
    <el-table
      stripe
      border
      v-loading="loading"
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      row-key="id"
    >
      <el-table-column
        label="考试名称"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="科目" align="center">
        <template slot-scope="scope">
          <km-tag :option="scope.row.subjectName"></km-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="考试模版"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <el-link
            v-if="scope.row.templateStatus == 0"
            type="danger"
            :underline="false"
            >未制作</el-link
          >
          <el-link
            v-if="scope.row.templateStatus == 1"
            type="primary"
            :underline="false"
            >制作中</el-link
          >
          <el-link
            v-if="scope.row.templateStatus == 2"
            type="success"
            :underline="false"
            >已完成</el-link
          >
        </template>
      </el-table-column>
      <el-table-column
        label="扫描进度"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <el-link
            v-if="scope.row.scannedNum == 0"
            type="danger"
            :underline="false"
            >{{ scope.row.scannedNum }}/{{ scope.row.scannedCount }}</el-link
          >
          <el-link
            v-if="
              scope.row.scannedNum > 0 &&
              scope.row.scannedNum != scope.row.scannedCount
            "
            type="primary"
            :underline="false"
            >{{ scope.row.scannedNum }}/{{ scope.row.scannedCount }}</el-link
          >
          <el-link
            v-if="
              scope.row.scannedNum != 0 &&
              scope.row.scannedNum == scope.row.scannedCount
            "
            type="success"
            :underline="false"
            >已完成</el-link
          >
        </template>
      </el-table-column>
      <el-table-column
        label="上传异常"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <el-link type="danger" :underline="false">{{
            scope.row.uploadExceptionNum
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        label="客观题异常"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <el-link type="danger" :underline="false">{{
            scope.row.objExceptionNum
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        label="任务分配"
        align="center"
        prop="tokenId"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.taskAssignmentStatus == 1" type="success"
            >已分配</el-tag
          >
          <el-tag v-if="scope.row.taskAssignmentStatus == 0" type="info"
            >待分配</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        label="评阅进度"
        align="center"
        prop="tokenId"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <div>
            <el-link
              v-if="scope.row.reviewedNum == 0"
              type="danger"
              :underline="false"
              >{{ scope.row.reviewedNum }}/{{ scope.row.reviewCount }}</el-link
            >
            <el-link
              v-if="scope.row.reviewedNum > 0"
              type="primary"
              :underline="false"
              >{{ scope.row.reviewedNum }}/{{ scope.row.reviewCount }}</el-link
            >
          </div>
          <div>
            <el-link
              v-if="scope.row.reviewStatus == 0"
              type="info"
              :underline="false"
              >评阅未开始</el-link
            >
            <el-link
              v-if="scope.row.reviewStatus == 1"
              type="primary"
              :underline="false"
              >评阅进行中</el-link
            >
            <el-link
              v-if="scope.row.reviewStatus == 2"
              type="danger"
              :underline="false"
              >评阅暂停中</el-link
            >
            <el-link
              v-if="scope.row.reviewStatus == 3"
              type="success"
              :underline="false"
              >评阅已完成</el-link
            >
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="评阅异常处理"
        align="center"
        prop="tokenId"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <el-link type="danger" :underline="false">{{
            scope.row.reviewExceptionNum
          }}</el-link>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="仲裁处理"
        align="center"
        prop="tokenId"
        :show-overflow-tooltip="true"
      /> -->
      <el-table-column
        label="后传数"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <el-link type="warning" :underline="false">
            {{ scope.row.postCard }}/{{ scope.row.passCardAll }}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        v-if="$auth.hasRole('admin')"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-link type="primary" @click="edit(scope.row)">编辑</el-link>
          <el-link type="danger" @click="del(scope.row.id)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>

    <div class="page-bottom">
      <div class="sel-box">
        <p>共 {{ total }} 项</p>
        <el-button type="primary" @click="edit(false)" v-hasRole="['admin']"
          >添加考试</el-button
        >
      </div>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <el-dialog
      ref="editDialog"
      class="editKmVisible"
      title="创建/编辑考试"
      :visible.sync="editKmVisible"
      width="600px"
      :before-close="beforeClose"
    >
      <el-table :data="editList" border style="width: 100%">
        <el-table-column label="考试名称">
          <template slot-scope="scope">
            <el-input v-model="scope.row.name"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="所属科目"
          ><template slot-scope="scope">
            <el-select v-model="scope.row.subjectId">
              <el-option
                v-for="dict in xklb"
                :key="dict.subjectId"
                :label="dict.subjectName"
                :value="dict.subjectId"
              />
            </el-select> </template
        ></el-table-column>
        <el-table-column label="满分">
          <template slot-scope="scope">
            <el-input v-model="scope.row.fullMark"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" v-if="!editList[0].id"
          ><template slot-scope="scope">
            <el-link
              v-if="editList.length != 1"
              type="danger"
              @click="remove(scope.$index)"
              >删除</el-link
            >
            <el-link
              v-if="scope.$index == editList.length - 1"
              type="primary"
              @click="add"
              >新增</el-link
            ></template
          ></el-table-column
        >
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">返 回</el-button>
        <el-button type="primary" @click="editSubmit" :loading="loading"
          >保 存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  subjectpage,
  subjectAdd,
  subjectEdit,
  subjectDel,
} from "@/api/xmgl/ksgl";
import { subjectlistselect, checkPassword } from "@/api/common";
import { groupDetail } from "@/api/jcgl/xkgl";
export default {
  props: {
    xmxx: {},
    qbxklb: null,
  },
  data() {
    return {
      loading: false,

      ids: [],

      total: 0,
      tableData: [],

      multiple: true,

      queryParams: {
        pageNum: 1,
        pageSize: 15,
      },

      editKmVisible: false,
      rules: {
        subjectName: [
          { required: true, message: "科目名称不能为空", trigger: "blur" },
        ],
      },

      xklb: [],
      editList: [
        {
          name: "",
          subjectId: "",
          subjectName: "",
          examId: this.xmxx.id,
        },
      ],
    };
  },
  created() {
    this.getList();
    let xklb = [];
    groupDetail(this.xmxx.selectType).then((res) => {
      console.info(res,'resssss')
      let examSubjects = res.data?res.data.examSubjects.split(","):[];
      let basicChooseSubject;
      if (res.data.basicChooseSubject) {
        basicChooseSubject = res.data.basicChooseSubject.map((item) => {
          return item.selectSubjectIds.split(",");
        });
        basicChooseSubject = basicChooseSubject.reduce((acc, curr) => {
          return acc.concat(curr);
        }, []);
        examSubjects = examSubjects.concat(basicChooseSubject);
      }

      this.qbxklb.map((item) => {
        if (examSubjects.indexOf(item.subjectId.toString()) != -1) {
          xklb.push(item);
        }
      });

      this.xklb = xklb;
    });
  },
  methods: {
    getList() {
      this.loading = true;
      this.tableData = [];
      subjectpage({ ...this.queryParams, examId: this.xmxx.id })
        .then((response) => {
          this.tableData = response.rows;
          this.total = response.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
    edit(row) {
      if (row) {
        this.editList = [
          {
            name: row.name,
            subjectId: row.subjectId,
            examId: row.examId,
            fullMark: row.fullMark,
            subjectName: "",
            id: row.id,
          },
        ];
      }
      this.editKmVisible = true;
    },
    add() {
      this.editList.push({
        name: "",
        subjectId: "",
        subjectName: "",
        fullMark: "",
        examId: this.xmxx.id,
      });
    },
    remove(index) {
      this.editList.splice(index, 1);
    },
    beforeClose(done) {
      this.editList = [
        {
          name: "",
          subjectId: "",
          subjectName: "",
          fullMark: "",
          examId: this.xmxx.id,
        },
      ];
      this.$nextTick(() => {
        done();
      });
    },
    handleClose() {
      this.beforeClose(() => {
        this.editKmVisible = false;
      });
    },

    editSubmit() {
      let varify = this.editList.some((item) => {
        return !item.subjectId || !item.name || !item.fullMark;
      });
      if (varify) {
        this.$modal.alertError("存在空数据，请填写完整或删除");
        return;
      }
      let mfVarify = this.editList.some((item) => {
        return !/^[0-9]*$/.test(item.fullMark);
      });
      if (mfVarify) {
        this.$modal.alertError("满分必须为纯数字");
        return;
      }

      this.loading = true;
      let req;
      let data = this.editList.map((item) => {
        return {
          ...item,
          subjectName: this.xklb.filter((v) => {
            return v.subjectId == item.subjectId;
          })[0].subjectName,
        };
      });
      if (this.editList[0].id) {
        req = subjectEdit;
        data = data[0];
      } else {
        req = subjectAdd;
      }

      req(data)
        .then((res) => {
          this.$modal.msgSuccess("操作成功");
          this.handleClose();
          this.getList();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    del(id) {
      this.$prompt(
        "删除考试后，可能造成考试下级数据全部清空导致数据的缺失，请输入管理员密码以进行下步操作",
        "删除考试",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputPlaceholder: "请输入管理员密码",
          inputType: "password",
          inputValidator: (value) => {
            if (!value) {
              return "请输入密码";
            } else {
              return true;
            }
          },
        }
      )
        .then(({ value }) => {
          checkPassword({
            password: value,
            schoolId: this.$store.state.user.deptId,
          }).then((res) => {
            subjectDel(id).then((res) => {
              this.$modal.msgSuccess("操作成功");
              this.getList();
            });
          });
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
</style>